import requests
from lxml import etree
import csv
import os

"""
爬取城市省份
"""


# 初始化：创建文件
def init():
    if not os.path.exists('city.csv'):
        with open('city.csv', 'w', encoding='utf8', newline='') as f:
            writer = csv.writer(f)
            # 标题
            writer.writerow([
                'name',
                'city_link'
            ])


# 写入数据
def writer_row(row):
    with open('city.csv', 'a', encoding='utf8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(row)


# 爬取网页
def get_html(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
                      "Chrome/125.0.0.0 Safari/537.36",
        "Cookie": """QN1=00013a80306c6097a8f84b0b; QN99=7575; qunar-assist={%22version%22:%2220211215173359.925%22%2C%22show%22:false%2C%22audio%22:false%2C%22speed%22:%22middle%22%2C%22zomm%22:1%2C%22cursor%22:false%2C%22pointer%22:false%2C%22bigtext%22:false%2C%22overead%22:false%2C%22readscreen%22:false%2C%22theme%22:%22default%22}; QN269=6A0222B020E111EFA884C68241B7B338; fid=3854ee48-b5d9-4db4-939b-729b0d854dac; QN277=s%3Dbing; QN48=tc_c652c6704bf0bad7_18fd912ac32_2157; QN300=s%3Dbing; QN205=s%3Dbing; QN601=5cccfb678265181e61e4e020915750d9; ariaDefaultTheme=null; quinn=434bffd7f0dc297385480c1911d2e2dbf0717f3301ebedf049150f058afb9a4571430528114ccba477d0187067bbdd37; QN57=17173346638190.9571355881571146; QunarGlobal=10.68.204.216_-51a614aa_18f7c8916a5_6032|1717334665766; QNSPU=4065721161; ctt_june=1683616182042##iK3wasanVuPwawPwasaOW2kIVK0GERjwX%3DGDaSDwVRoDX2WGWK0hER38XPX8iK3siK3saKgnWsa%2BVKvAWKjOahPwaUvt; ctf_june=1683616182042##iK3wWSjsWwPwawPwa%3DjAW2j%3DaSGRXSHGW2aAWPXAWK3sWSXsEPPAaRXOEDP%3DiK3siK3saKgnWsa%2BVKvAWKj%2BahPwaUvt; cs_june=f6eb45fb177ffc7b1a1adcaf4660700ac0aa2dd64e1fd40ba7c431a5a772eaea204e2da782fb1b0b8373bd775579573192de14385b88bb72b63f8d0d3f1253ddb17c80df7eee7c02a9c1a6a5b97c11793f60b97952cd65ddcf77dc3cf9a2cf7c5a737ae180251ef5be23400b098dd8ca; QN271AC=register_pc; QN271SL=82ff5934cad5e560a6bfcaec8c37203c; QN271RC=82ff5934cad5e560a6bfcaec8c37203c; _q=U.uyfetru1921; csrfToken=vu3zgaCwhJ3g6fucnulczWdzfpbpbxv1; _s=s_GFG76Z4DKCE4GE7K6LKDXHLFPM; _t=28732424; _v=g5-1QWto2OobEtgP816t8cP3_jTE2_1kq2vwB4mVnkIC8LC4ORuq9M2a4Rw3xGqtM0mvMbG4gQVHIMgezGdMXgvP5-iewvn1H0OfYnhDW23JTtYUJJoxcPDVWvqTC6ILYQcuDJrKQIYVcvJiUCs7uw_GOMW6Vef37HgSQd2nwrhE; QN43=""; QN42=%E5%8E%BB%E5%93%AA%E5%84%BF%E7%94%A8%E6%88%B7; QN71="MTEzLjE2LjI0NS44MDrljZflroE6MQ=="; QN44=uyfetru1921; _i=DFiEuYALRrSwIKi6tc2mTe5qDK6w; _vi=YSrW74qdVST_ClmD6YvGbHQSZSaqBNFx1jsH1L60-7ottBiqrR8EncrQlO5oY6sXO2ulUAJ0aKUaduanioVQPt66cYZLfB47Lsw4FIFhHJo7EQ9YPOwDAU816c9ieJqu9Kcpau1Xn8te9dIkqOP3yuBelzdz0i5dKh60S6t3Moxf; QN58=1717379098256%7C1717379098256%7C1; JSESSIONID=103D52B0D114717D1C75D9FA7A949EB1; QN271=0d7f3354-d72b-44af-8320-03345122e820; QN267=310146450e5738b8c; __qt=v1%7CVTJGc2RHVmtYMS9OMkE5alo5M2UydW45WFo3ZXVvVlJraENuSFQrTDRVVm40M1orZ0RBZDUxeXhTVzVaU2JFa1Zrckl4RmhtZ3pnMGtHMDRFSnJQTDZKcTF6OS9reGhoQlNDZTRHNmw2WDViRkQ5c3dMOFMvZ01TOXErRG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QdVBuWTkzMjJhRTE2Ujl6aGhoNUpaRVJXQ1c4YXd1ZTlUOFdJVUxSa3Juc0J4Yk1YMTVRd09RYlBpNHBvNXFQbVJvRkI4VUgyRHRsOTlRZWxIckplb0lmRUE3OEdFVktsYlA3M0RsUS9XVEdLZjdkZ2FMbHZINmZrK21xbk1ROVRjVm44"""
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response
    else:
        return None


# 解析html
def parse_html(response):
    root = etree.HTML(response.text)
    city_list = root.xpath('//div[@class="mp-city-content"]//li[@class="mp-city-item"]/a')
    for city in city_list:
        city_name = city.text
        city_link = "https://piao.qunar.com/ticket/list.htm?keyword=%s" % city_name
        writer_row([
            city_name,
            city_link
        ])


if __name__ == '__main__':
    url = "https://piao.qunar.com/daytrip/list.htm"
    init()
    response = get_html(url)
    parse_html(response)
